package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.DeviceDataPageReqDto;
import com.zzyl.mapper.DeviceDataMapper;
import com.zzyl.service.DeviceDataService;
import com.zzyl.vo.DeviceDataGraphVo;
import com.zzyl.vo.DeviceDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description DeviceDataServiceImpl
 * @Author cuijian
 * @Date 2024-09-14
 */
@Service
public class DeviceDataServiceImpl implements DeviceDataService {

    @Autowired
    private DeviceDataMapper deviceDataMapper;

    /**
     * 条件分页查询设备数据
     *
     * @param deviceDataPageReqDto
     * @return
     */
    @Override
    public PageResponse<DeviceDataVo> selectByPage(DeviceDataPageReqDto deviceDataPageReqDto) {
        PageHelper.startPage(deviceDataPageReqDto.getPageNum(), deviceDataPageReqDto.getPageSize());
        //前端传递的是一个lang类型的毫秒值,作为条件查询数据库要专函为日期类型
        Long startTime = deviceDataPageReqDto.getStartTime();
        Long endTime = deviceDataPageReqDto.getEndTime();
        deviceDataPageReqDto.setStartDate(ObjectUtil.isEmpty(startTime) ? null : LocalDateTimeUtil.of(startTime));
        deviceDataPageReqDto.setEndDate(ObjectUtil.isEmpty(endTime) ? null : LocalDateTimeUtil.of(endTime));
        //查询
        Page<DeviceDataVo> page = deviceDataMapper.selectByPage(deviceDataPageReqDto);

        return PageResponse.of(page, DeviceDataVo.class);
    }

    /**
     * 按天统计每小时查询设备数据
     *
     * @param iotId
     * @param functionId
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByDay(String iotId, String functionId, Long startTime, Long endTime) {
        //1.将lang类型时间挫转换为日期类型 localDateTime
        LocalDateTime startTimeObj = LocalDateTimeUtil.of(startTime);
        LocalDateTime endTimeobj = LocalDateTimeUtil.of(endTime);
        List<DeviceDataGraphVo> dbDataGraphVoList = deviceDataMapper.queryDeviceDataListByDay(iotId, functionId, startTimeObj, endTimeobj);
        //将集合中的数据转换为map(key=dateTime,value=dataValue)
        Map<String, Double> map = dbDataGraphVoList.stream().collect(Collectors.toMap(DeviceDataGraphVo::getDateTime, DeviceDataGraphVo::getDataValue));

        //2.调用DeviceDataGraphVo的静态方法,dayInstance生成24小时数据Vo列表集合(里面所有的值都为0)
        List<DeviceDataGraphVo> deviceDataGraphVo24List = DeviceDataGraphVo.dayInstance(startTimeObj);
        //3.遍历24小时数据Vo列表集合,将map中的值赋给对应的Vo
        deviceDataGraphVo24List.forEach(deviceDataGraphVo -> {
            Double value = map.get(deviceDataGraphVo.getDateTime());
            if (value != null) {
                deviceDataGraphVo.setDataValue(value);
            }
        });
        //4.返回集合
        return deviceDataGraphVo24List;
    }

    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByWeek(String iotId, String functionId, Long startTime, Long endTime) {
        //1.将lang类型时间挫转换为日期类型 localDateTime
        LocalDateTime startTimeObj = LocalDateTimeUtil.of(startTime);
        LocalDateTime endTimeobj = LocalDateTimeUtil.of(endTime);
        List<DeviceDataGraphVo> dbDataGraphVoList = deviceDataMapper.queryDeviceDataListByWeek(iotId, functionId, startTimeObj, endTimeobj);
        //将集合中的数据转换为map(key=dateTime,value=dataValue)
        Map<String, Double> map = dbDataGraphVoList.stream().collect(Collectors.toMap(DeviceDataGraphVo::getDateTime, DeviceDataGraphVo::getDataValue));
        //2.调用DeviceDataGraphVo的静态方法,dayInstance生成一周的数据Vo列表集合(里面所有的值都为0)
        List<DeviceDataGraphVo> weekedInstance = DeviceDataGraphVo.weekInstance(startTimeObj);
        //3.遍历24小时数据Vo列表集合,将map中的值赋给对应的Vo
        weekedInstance.forEach(deviceDataGraphVo -> {
            Double value = map.get(deviceDataGraphVo.getDateTime());
            if (value != null) {
                deviceDataGraphVo.setDataValue(value);
            }
        });
        //4.返回集合
        return weekedInstance;
    }
}